﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace CJJ.Common.Validator
{
    /// <summary>
    /// 验证元素值是否大于或者小于指定数值number
    /// </summary>
    [Serializable]
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
    public class MoreThanZeroAttribute : BaseAttribute
    {
        /// <summary>
        /// 当前比较值
        /// </summary>
        private decimal _value;

        /// <summary>
        /// 是否包含了改值
        /// </summary>
        private bool _isIncludeValue;
        /// <summary>
        /// 是否是小于
        /// </summary>
        private bool _isLessThan { get; set; }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="value">用于比较的值</param>
        /// <param name="isIncludeValue">是否包含该值</param>
        /// <param name="isLessThan">是否是小于</param>
        /// <param name="msg">返回的错误信息</param>
        /// <param name="group">分组，用于解决同一个参数的校验方式在不同业务中使用不同规则</param>
        public MoreThanZeroAttribute(int value,  bool isLessThan, bool isIncludeValue, string msg, string @group = "") : base(msg, @group)
        {
            _value = value;
            _isLessThan = isLessThan;
            _isIncludeValue = isIncludeValue;
        }

        /// <summary>
        /// 验证参数是否符合要求
        /// </summary>
        /// <param name="value">参数值</param>
        /// <param name="prop">参数类型</param>
        /// <returns>符合要求=true</returns>
        public override bool Validate(object value, PropertyInfo prop)
        {
            decimal dec = Convert.ToDecimal(value);
            if (_isLessThan)
            {
                if (_isIncludeValue)
                {
                    return dec <= _value;
                }
                else
                {
                    return dec <= _value;
                }
            }
            else
            {
                if (_isIncludeValue)
                {
                    return dec >= _value;
                }
                else
                {
                    return dec >= _value;
                }
            }

        }
    }
}
